package com.bw.purchase.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bw.common.core.utils.StringUtils;
import com.bw.common.redis.service.RedisService;
import com.bw.common.security.utils.SecurityUtils;
import com.bw.model.LoginUser;
import com.bw.pojo.PmsSku;
import com.bw.purchase.mapper.PurchaseMapper;
import com.bw.purchase.pojo.Do.PurchaseDo;
import com.bw.purchase.pojo.Dto.PurchaseDto;
import com.bw.purchase.pojo.Vo.PurchaseVo;
import com.bw.purchase.pojo.Vo.SkuPurchaseVo;
import com.bw.purchase.service.PurchaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private PurchaseMapper purchaseMapper;

    /**
     * 添加订单记录表
     * @param purchaseDo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(PurchaseDo purchaseDo) {
        //校验
        if(StringUtils.isNull(purchaseDo.getSuppliersId())){
            throw new RuntimeException("请选择供应商");
        }
        if (purchaseDo.getSkuPurchases().size() == 0){
            throw new RuntimeException("请选择要购买的商品");
        }
        //添加订单记录
        purchaseDo.setStatus(0);
        purchaseMapper.addPurchaseList(purchaseDo);
        //遍历
        purchaseDo.getSkuPurchases().forEach(skuPurchases -> {
            //校验
            if(StringUtils.isNull(skuPurchases.getPurchaseNum())){
                throw new RuntimeException("请填写需要购买的商品数量");
            }
            if(skuPurchases.getStatus() != null){
                if(skuPurchases.getStatus() == 1){
                    throw new RuntimeException("编号"+skuPurchases.getSkuId()+"正在采购");
                }
            }
            //添加商品订单中间表
            skuPurchases.setStatus(1);
            purchaseMapper.addSkuPurchase(purchaseDo.getId(),skuPurchases.getSkuId(),skuPurchases.getPurchaseNum(),skuPurchases.getStatus());
        });
    }


    /**
     * 查询审核订单列表
     * @param purchaseDto
     * @return
     */
    @Override
    public List<PurchaseVo> selectPurchaseList(PurchaseDto purchaseDto) {
        List<PurchaseVo> list = purchaseMapper.selectPurchaseList(purchaseDto);
        return list;
    }

    /**
     * 采购单审核
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePurchaseStatus(Integer id) {
        //更新审核状态
        purchaseMapper.updatePurchaseStatus(id);
        //查询审核表信息
        List<SkuPurchaseVo> skuPurchaseVo = purchaseMapper.selectSkuPurchase(id);
        skuPurchaseVo.forEach(skuPurchase -> {
            //删除中间表
            purchaseMapper.deleteSkuPurchase(id);
            //修改商品数量
            Integer purchaseNum = skuPurchase.getPurchaseNum();
            purchaseMapper.updatePurchaseNum(skuPurchase.getSkuId(),purchaseNum);
        });




    }

    /**
     * 预警商品列表展示
     * @return
     */
    @Override
    public List<PmsSku> warningSkuList() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<PmsSku> list = purchaseMapper.pmsSkuList(loginUser.getUserid());
        return list;
    }
}